package com.guanyong.services;

import com.guanyong.dao.IVideoTodayDaoMapper;
import com.guanyong.dao.IVideoTopLogDaoMapper;
import com.guanyong.model.VideoTodayModel;
import com.guanyong.model.VideoTopLogModel;
import com.guanyong.utils.Comm.ResultCode;
import com.guanyong.utils.Comm.ResultObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Administrator on 2017/6/27 0027.
 */
@Service
public class VideoTodayService {

    @Autowired
    IVideoTodayDaoMapper iVideoTodayDaoMapper;

    @Autowired
    ObjectClassTypeService objectClassTypeService;

    public ResultObject<Integer> AddVideoToday(VideoTodayModel videoTodayModel){
        ResultObject<Integer> resultObject=new ResultObject<Integer>();
        Integer res=-1;
        if (iVideoTodayDaoMapper.getobjectidCount(videoTodayModel.getfObjectid())>0){
            resultObject.setCode(ResultCode.OperateFail);
            resultObject.setErrorMsg("电影已添加");
        }else{
            res =iVideoTodayDaoMapper.insert(videoTodayModel);
        }

        if (res >-1){
                resultObject.setCode(ResultCode.OperateOk);
        }else{
            resultObject.setCode(ResultCode.OperateFail);
            resultObject.setErrorMsg("添加失败！");
        }
        return resultObject;
    }


    public Integer getVideoTopLogCount(Integer classType){

        return iVideoTodayDaoMapper.getCount();
    }

    @Transactional
    public ResultObject<Integer> delVideoToday(Integer fid){
        ResultObject<Integer> resultObject=new ResultObject<Integer>();
        VideoTodayModel videoTodayModel = iVideoTodayDaoMapper.getByIdVideoToday(fid);
        if (videoTodayModel!=null){
            Integer res1 = iVideoTodayDaoMapper.del(fid);
            if (res1>-1 ){
                resultObject.setCode(ResultCode.OperateOk);
            }else{
                resultObject.setCode(ResultCode.OperateFail);
            }
        }else{
            resultObject.setCode(ResultCode.OperateFail);
        }

        return resultObject;
    }
//
//
//    //2017年9月19日 17:22:41  修改完成
    //推荐列表
    public HashMap<String,Object> getVideoTodayList(Integer classType,
                                                     Integer pno,
                                                     Integer pagesize,
                                                     Integer allcount){
        HashMap<String,Object> hashMap=new HashMap();
        if (allcount<0){
            Integer count=iVideoTodayDaoMapper.SelectVideoTodayListCount(classType);
            hashMap.put("count",count);
        }
        else {
            hashMap.put("count",allcount);
        }
        List<VideoTodayModel> videoTodayModellList =iVideoTodayDaoMapper.SelectVideoTodayList
                (classType,(pno-1)*pagesize,pagesize);

        for (VideoTodayModel videoTodayModel :videoTodayModellList){
            Object object= objectClassTypeService.GetObject(videoTodayModel
                            .getfClasstype(),
                    videoTodayModel.getfObjectid());
            if (object!=null) {
                videoTodayModel.setfObject(object);
            }

        }
        hashMap.put("list",videoTodayModellList);
             return hashMap;
    }
//
    @Transactional
    public ResultObject<Integer> upVideoToday(Integer fid){
        ResultObject<Integer> resultObject=new ResultObject<Integer>();
        Integer res=-1;
        VideoTodayModel videoTodayModel =iVideoTodayDaoMapper.getByIdVideoToday(fid);
        videoTodayModel.setfIntime(new Date());
        Integer res1  =iVideoTodayDaoMapper.up(videoTodayModel);
        if (res1>-1){
            resultObject.setCode(ResultCode.OperateOk);
        }else{
            resultObject.setCode(ResultCode.OperateFail);
        }
        return resultObject;
    }
}
